/**
 * 是否为空值
 * @param data
 */
export const notNull = (data: any): boolean => {
	return ![null, undefined, NaN, ''].includes(data);
};

/**
 * 获取数据类型
 * @param data
 * @returns
 */
export const getType = (data: any): string => {
	return {}.toString.call(data).slice(8, -1).toLocaleLowerCase();
};

/**
 * 数据是否为指定类型
 * @param data
 * @param type
 * @returns
 */
export const isType = (data: any, type: string): boolean => {
	return getType(data) === type;
};

/**
 * 是否为空
 */
export const isEmpty = (data: any) => {};

/**
 * 是否有属性
 * @param data
 * @param prop
 * @returns
 */
export const hasProperty = (data: any, prop: string): boolean => {
	return {}.hasOwnProperty.call(data, prop);
};

/**
 * 是否为promise
 * @param data
 * @returns
 */
export const isPromise = (data: any): boolean => {
	return (
		data !== null &&
		(typeof data === 'object' || typeof data === 'function') &&
		typeof data.then === 'function'
	);
};

// 展开promise
type IDefer = {
	resolve: Function;
	reject: Function;
	promise: Promise<any>;
};

/**
 * promise展开
 * @returns
 */
export const defer = (): IDefer => {
	// @ts-ignore
	const res = {};
	const promise = new Promise((resolve, reject) => {
		// @ts-ignore
		res.resolve = resolve;
		// @ts-ignore
		res.reject = reject;
	});
	// @ts-ignore
	res.promise = promise;
	return res as IDefer;
};
